package hu.uszeged.inf.wlab.stunner.screens.bookmarks.adapter;

import hu.uszeged.inf.wlab.stunner.R;
import hu.uszeged.inf.wlab.stunner.database.dtos.DiscoveryDTO;

import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * {@link BaseAdapter} implementation to populate the discovery list from the supplied data.
 * 
 * @author szelezsant
 */
public class BookmarkAdapter extends BaseAdapter {

	/**
	 * The list of discovery dtos; the source of data.
	 */
	private final List<DiscoveryDTO> data;

	/**
	 * Constructor.
	 * 
	 * @param data - the data source
	 */
	public BookmarkAdapter(final List<DiscoveryDTO> data) {
		super();
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(final int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(final int position) {
		return data.get(position).getIdentifier();
	}

	@Override
	public View getView(final int position, final View convertView, final ViewGroup parent) {
		RelativeLayout returnView = (RelativeLayout) convertView;
		if (null == returnView) {
			returnView = (RelativeLayout) ((LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
					R.layout.bookmark_row, parent, false);
			final ViewHolder holder = new ViewHolder();
			holder.textViewAddress = (TextView) returnView.findViewById(R.id.textViewAddress);
			holder.textViewDate = (TextView) returnView.findViewById(R.id.textViewDate);
			holder.textViewResult = (TextView) returnView.findViewById(R.id.textViewResult);
			holder.textViewLocalIP = (TextView) returnView.findViewById(R.id.textViewLocalIP);
			holder.textViewPublicIP = (TextView) returnView.findViewById(R.id.textViewPublicIP);
			returnView.setTag(holder);
		}

		final DiscoveryDTO discovery = data.get(position);
		final ViewHolder holder = (ViewHolder) returnView.getTag();
		holder.textViewDate.setText(DateFormat.format(parent.getContext().getString(R.string.date_format), discovery.getTimeStamp()));
		holder.textViewAddress.setText(discovery.getLocation());
		holder.textViewResult.setText(discovery.getDiscoveryResult().getResourceId());
		holder.textViewLocalIP.setText(discovery.getLocalIP());
		holder.textViewPublicIP.setText(discovery.getPublicIP());

		return returnView;
	}

	/**
	 * Utility class to hold the view references of the row.
	 * 
	 * @author szelezsant
	 */
	static class ViewHolder {
		/** TextView to display the date. */
		private TextView textViewDate;
		/** TextView to display the address. */
		private TextView textViewAddress;
		/** TextView to display the discovery result. */
		private TextView textViewResult;
		/** TextView to display the local IP. */
		private TextView textViewLocalIP;
		/** TextView to display the public IP. */
		private TextView textViewPublicIP;
	}

}
